const moment = require('moment');

let redisKey = {};
let userLevel = {};
let superListArr = {};
let limitScoreListArr = {};

class redisService {


    static getInstance() {
        if (!redisService.instance) {
            redisService.instance = new redisService();
        }
        return redisService.instance;
    }



    //判断是否锁定
    isLocked(myKey, timeOut = 3) {

        if (redisKey[myKey]) {
            if (moment().isAfter(redisKey[myKey])) {
                delete redisKey[myKey];
                redisKey[myKey] = moment().add(timeOut, 's');
                return false;
            } else {
                return true;
            }
        } else {
            redisKey[myKey] = moment().add(timeOut, 's');
            return false;
        }
    }

    //解除锁定
    unLock(myKey) {

        if (redisKey[myKey]) {
            delete redisKey[myKey];
        }
    }


    //加入游戏层级判断
    addUserLevel(uid, unionID, levelInfo) {
        userLevel[uid + '-' + unionID] = levelInfo;
    }


    //获取玩家等级
    getUserLevel(uid, unionID) {

        return userLevel[uid + '-' + unionID];

    }

    addUserSuper(uid, unionID, superList, endTime) {

        superListArr[uid + '-' + unionID] = { superList, endTime };
    }

    //返回上级列表
    getUserSuper(uid, unionID) {

        let curSuper = superListArr[uid + '-' + unionID];
        if (curSuper && curSuper.endTime > moment().format('x')) {
            return curSuper.superList;
        } else {
            return null;
        }
    }

    //保存数据
    addUserLimitScore(uid, unionID, limitScore, totalScore, endTime) {

        limitScoreListArr[uid + '-' + unionID] = { limitScore, totalScore, endTime };
    }

    //获取数据
    getUserLimitScore(uid, unionID) {

        let curSuper = limitScoreListArr[uid + '-' + unionID];
        if (curSuper && curSuper.endTime > moment().format('x')) {
            return { limitScore: curSuper.limitScore, totalScore: curSuper.totalScore };
        } else {
            return null;
        }
    }

}

module.exports = redisService.getInstance();
